package org.jetlinks.community.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.hswebframework.utils.StringUtils;
import org.jetlinks.community.command.CommandEnum;
import org.jetlinks.community.entity.DeviceBizMessage;
import org.jetlinks.community.entity.DeviceBizMessageReply;
import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.event.EventMessage;
import org.jetlinks.core.message.function.FunctionInvokeMessage;
import org.jetlinks.core.message.function.FunctionInvokeMessageReply;
import org.jetlinks.core.message.function.FunctionParameter;
import org.jetlinks.core.message.property.ReportPropertyMessage;

import java.util.*;

public class BizMessageUtil {
    public static FunctionInvokeMessage buildFunctionInvokeMessage(DeviceBizMessage deviceBizMessage){
        String deviceId=deviceBizMessage.getDeviceId();
        String productCode=deviceBizMessage.getProductCode();
        String cmd=deviceBizMessage.getCmd();
        String messageId=deviceBizMessage.getMessageId();
        Long messageTime=deviceBizMessage.getMessageTime();
        Map<String,Object> data=deviceBizMessage.getData();
        FunctionInvokeMessage invokeMessage=new FunctionInvokeMessage();
        invokeMessage.setDeviceId(deviceId);
        invokeMessage.setFunctionId(cmd);
        invokeMessage.setMessageId(messageId);
        invokeMessage.setTimestamp(messageTime);
        List<FunctionParameter> inputs = new ArrayList();
        if(!StringUtils.isNullOrEmpty(data)){
            Set keySet=data.keySet();
            Iterator iterator=keySet.iterator();
            while (iterator.hasNext()){
                String key=iterator.next().toString();
                Object value=data.get(key);
                FunctionParameter param = new FunctionParameter();
                param.setName(key);
                param.setValue(value);
                inputs.add(param);
            }
            invokeMessage.setInputs(inputs);
        }
        return invokeMessage;
    }

    public static DeviceBizMessageReply buildBizMsgReplyFromReportPropertyMessage(ReportPropertyMessage reportPropertyMessage){
        String enumType=reportPropertyMessage.getCode();
        String deviceId=reportPropertyMessage.getDeviceId();
        String messageId=reportPropertyMessage.getMessageId();
        Map<String, Object> map = reportPropertyMessage.getProperties();//.getData();
        DeviceBizMessageReply reply = new DeviceBizMessageReply();
        reply.setData(map);
        reply.setCmd(enumType);
        reply.setMessageId(messageId);
        reply.setDeviceId(deviceId);
        reply.setReplyMessageId(messageId);
        reply.setNeedCallBack(false);

        return reply;
    }

    public static DeviceBizMessageReply buildBizMsgReplyFromEventMessage(EventMessage eventMessage){
        String enumType=eventMessage.getEvent();
        String deviceId=eventMessage.getDeviceId();
        String messageId=eventMessage.getMessageId();
        Map<String, Object> map = (Map<String, Object>)eventMessage.getData();
        DeviceBizMessageReply reply = new DeviceBizMessageReply();
        reply.setData(map);
        reply.setCmd(enumType);
        reply.setMessageId(messageId);
        reply.setDeviceId(deviceId);
        reply.setReplyMessageId(messageId);
        reply.setNeedCallBack(false);

        return reply;
    }

    public static DeviceBizMessageReply buildBizMsgReplyFromFunctionInvokeMessageReply(FunctionInvokeMessageReply functionInvokeMessageReply){
        String enumType=functionInvokeMessageReply.getFunctionId();
        String deviceId=functionInvokeMessageReply.getDeviceId();
        String messageId=functionInvokeMessageReply.getMessageId();
        Map<String, Object> map = (Map<String, Object>)functionInvokeMessageReply.getOutput();
        DeviceBizMessageReply reply = new DeviceBizMessageReply();
        reply.setData(map);
        reply.setCmd(enumType);
        reply.setMessageId(messageId);
        reply.setDeviceId(deviceId);
        reply.setReplyMessageId(messageId);
        reply.setNeedCallBack(true);

        return reply;
    }
}
